#!/bin/bash

# 检查命令是否存在
command_exists() {
    command -v "\$1" >/dev/null 2>&1
}

# 检查是否安装了davfs2
if ! command_exists davfs2; then
    echo "请先安装davfs2"
    exit 1
fi

# 检查参数数量
if [ $# -ne 3 ]; then
    echo "参数错误！"
    echo "用法: \$0 [挂载|卸载] 本地挂载点 [用户名:密码@网盘地址:端口]"
    exit 1
fi

action=\$1
mount_point=\$2
credentials=\$3

# 检查挂载点是否存在
if [ ! -d "$mount_point" ]; then
    echo "挂载点不存在！"
    exit 1
fi

# 挂载网盘
if [ "$action" = "挂载" ]; then
    echo "正在挂载网盘到 $mount_point"
    echo "$credentials" | sudo mount -t davfs -o rw,file_mode=600,dir_mode=700 "$mount_point"
    if [ $? -eq 0 ]; then
        echo "挂载成功！"
    else
        echo "挂载失败！"
        exit 1
    fi
# 卸载网盘
elif [ "$action" = "卸载" ]; then
    echo "正在卸载网盘 $mount_point"
    sudo umount "$mount_point"
    if [ $? -eq 0 ]; then
        echo "卸载成功！"
    else
        echo "卸载失败！"
        exit 1
    fi
else
    echo "不支持的操作！"
    exit 1
fi

exit 0

